home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5217 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  91 lines

  1. Path: gidora.kralizec.net.au!root
  2. From: rosko@zeta.org.au (Ross McKay)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Watcom: corrupt this-pointer
  5. Date: Sat, 03 Feb 1996 02:26:10 GMT
  6. Organization: Soft Technologies
  7. Message-ID: <4eudvi$e84@gidora.kralizec.net.au>
  8. References: <31108A0B.108DC964@sp.zrz.tu-berlin.de>
  9. Reply-To: rosko@zeta.org.au
  10. NNTP-Posting-Host: dialup78.syd1.zeta.org.au
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. (copy to emai)
  14.  
  15. Olaf Lenzmann <olafbaje@sp.zrz.tu-berlin.de> wrote:
  16. >Hi there !
  17.  
  18. >I have a really confusing problem with Watcom-C++ 10.0:
  19.  
  20. >I have a cpp-Application with lots of self-defined classes,
  21. >compiled with a large memory model. When I create any of
  22. >my objects dynamically, the constructor seems to use a
  23. >corrput segment adress for the this-pointer. The offset
  24. >is ok, but anyway it results in a zillion general protection
  25. >faults. Borland C++ 4.52 does compile it allright though.
  26.  
  27. >Any ideas ? I tried about every compiler switch possible...
  28.  
  29. >        Thanx, olaf
  30.  
  31. Hi Olaf, I suspect one of the following:
  32.  
  33. a)   you have corrupted the program stack somewhere; 
  34.      a common way to do that is to return a pointer
  35.      to a local (temporary) object, which is really
  36.      a pointer to a place on the stack, and used it
  37.      after returning from the function the object
  38.      was defined in.
  39.  
  40. b)   you have an uninitialized or incorrectly assigned
  41.      pointer to an object, which you dereference to
  42.      access either a method of the object or a method
  43.      of one of its member objects. 
  44.  
  45. e.g. a)
  46.  
  47. int* F(int a)
  48. {
  49.     int b;
  50.     b = a;
  51.     return &b;    //  <-- there be Evil!!
  52. }
  53.  
  54. ....
  55.     int* c = F(2);
  56.     sprintf (buffer, "%d\n", *c);
  57. ....
  58.  
  59. e.g. b)
  60.     class SomeClass
  61.     {
  62.         public:
  63.             SomeClass (void) {}
  64.             void DoSomething (void);
  65.             void DoSomethingElse (void) { this->c.Crash(); }
  66.         private:
  67.             SomeOtherClass c;
  68.     };
  69.  
  70. e.g. b.1)
  71.  
  72.     SomeClass* a;        //  <-- uninitialized!!
  73.     a->DoSomething();    //  <-- roll, crash, burn!!
  74.  
  75. e.g. b.2)
  76.  
  77.     SomeClass* a;            //  <-- uninitialized!!
  78.     a->DoSomethingElse();    //  <-- this pointer trashed in 
  79.                              //      SomeOtherClass
  80.  
  81. Hope this helps.
  82. Cheers,
  83. ------------------------------------------------------------------
  84. Ross McKay        | snail: GPO Box 562, Sydney NSW 2001 Australia
  85. Soft Technologies | email: mailto:rosko@zeta.org.au
  86. Sydney, Australia |   URL: http://www.zeta.org.au/~rosko
  87. ------------------------------------------------------------------
  88. The opinions expressed are my own, not those of Soft Technologies.
  89.     "The beatings will continue, until staff morale improves."
  90.  
  91.